package com.cug.nageoffer.controller;

import com.cug.nageoffer.model.entity.ChatUser;
import com.cug.nageoffer.model.vo.ChatUserVo;
import com.cug.nageoffer.model.vo.Result;
import com.cug.nageoffer.model.vo.ResultCodeEnum;
import com.cug.nageoffer.model.vo.UserMsgVo;
import com.cug.nageoffer.service.ChatUserService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.websocket.server.PathParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping(value = "/api/chat/user")
public class ChatUserController {

    @Autowired
    private ChatUserService chatUserService;

    @Operation(summary = "初始化聊天用户")
    @GetMapping("/init")
    public Result init(HttpServletRequest request){
        HttpSession session = request.getSession();
        chatUserService.init(session);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据用户用户名查询聊天记录")
    @GetMapping(value = "/getHistoryByUsername")
    public Result<List<UserMsgVo>> getHistoryByUsername(
            @RequestParam(value = "username") String username ,
            @RequestParam(value = "senderUsername") String senderUsername){

        log.info("查询聊天记录，当前用户：{}，目标用户：{}",username,senderUsername);
        List<UserMsgVo>list = chatUserService.getHistoryByUsername(username,senderUsername);

        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据用户用户名查询聊天列表")
    @GetMapping(value = "/getChatListByUsername")
    public Result<List<ChatUserVo>> getChatListByUsername(
            @RequestParam("username") String username
    ){
        log.info("查询用户：{}聊天列表",username);
        List<ChatUserVo> list = chatUserService.getChatListByUsername(username);
        log.info("查询到用户：{},聊天列表：{}",username,list);
        return Result.build(list,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "保存聊天列表")
    @PostMapping(value = "/saveChatList")
    public Result saveChatList(@RequestBody ChatUser chatUser){
        log.info("保存聊天列表：{}",chatUser);
        chatUserService.saveChatList(chatUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
